<template>
	<view class="container">
		<!-- #ifndef MP -->
		
		<!-- #endif -->
		<view class="tui-page-title">登录</view>
		<view class="tui-form">
			<view class="tui-view-input">
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="mobile" color="#6d7a87" :size="20"></tui-icon>
					
						<input
							:adjust-position="false"
							:value="mobile"
							placeholder="请输入手机号"
							placeholder-class="tui-phcolor"
							type="number"
							maxlength="11"
							@input="inputMobile"
						/>
						<view class="tui-icon-close" v-show="mobile" @tap="clearInput(1)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="pwd" color="#4d2574" :size="20"></tui-icon>
						<input :value="password" placeholder="请输入密码" :password="true" placeholder-class="tui-phcolor" type="text" maxlength="40" @input="inputPwd" />
						<view class="tui-icon-close" v-show="password" @tap="clearInput(2)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
			</view>
			
			<view class="tui-btn-box"><tui-button :disabledGray="true" :disabled="disabled" :shadow="true" shape="circle" @tap="login">登录</tui-button></view>
			<view class="tui-cell-text">
				<view class="tui-color-primary" hover-class="tui-opcity" :hover-stay-time="150" @tap="href(1)">忘记密码？</view>
				<view hover-class="tui-opcity" :hover-stay-time="150">
					没有账号？
					<text class="tui-color-primary" @tap="href(2)">注册</text>
				</view>
			</view>
		</view>
		<view class="tui-login-way" ><view hover-class="tui-opcity"  @tap="showOtherLogin">其他方式登录</view></view>
		<tui-bottom-popup :mask="false" backgroundColor="transparent" :show="true">
			<view class="tui-auth-login">
				<!-- #ifndef MP -->
				<!-- #endif -->
				<!-- #ifdef APP-PLUS || MP-WEIXIN || H5 -->
				<view class="tui-icon-platform" hover-class="tui-opcity" :hover-stay-time="150" @tap="wxlogin">
					<image src="/static/images/icon_wechat.png" class="tui-login-logo"></image>
				</view>
				<view class="tui-icon-platform" hover-class="tui-opcity" :hover-stay-time="150" @tap="href(3)">
					<image src="/static/images/sms.png" class="tui-login-logo"></image>
				</view>
				<!-- #endif -->
				<!-- #ifndef MP -->
				<!-- #endif -->
			</view>
		</tui-bottom-popup>
		<!-- #ifdef MP-WEIXIN -->
		<MiniappAuth :auth="auth" @userinfo="get_userinfo"></MiniappAuth>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
			<AppAuth :auth="auth" @check_login="check_login"></AppAuth>
		<!-- #endif -->
	</view>
</template>

<script>
import MiniappAuth from "@/components/auth/miniapp_auth.vue"
import AppAuth from "@/components/auth/app_auth.vue"
import tuiBottomPopup from "@/components/tui/tui-bottom-popup"
import tuiButton from "@/components/tui/tui-button"
import tuiIcon from "@/components/tui/tui-icon"
import tuiListCell from "@/components/tui/tui-list-cell"
import {H5Token} from '@/common/auth/h5_token.js'
let time = Date.parse(new Date()) / 1000
export default {
	computed: {
		disabled: function() {
			let bool = true;
			if (this.mobile && this.password) {
				bool = false;
			}
			return bool;
		}
	},
	components:{
		MiniappAuth,
		AppAuth,
		tuiBottomPopup,
		tuiButton,
		tuiIcon,
		tuiListCell
	},
	data() {
		return {
			mobile: '',
			password: '',
			popupShow: false,
			code:'',
			isLogin:'',
			auth: {
				is_name: false,
				is_address: false,
				is_phone: false,
			},
		};
	},
	onLoad(options) {
		
	},
	onShow() {
		let isLogin = uni.getStorageSync('token');
		
		// #ifdef MP-WEIXIN
		if(isLogin){
			
			
			setTimeout(() => {
				uni.showToast({
					title:'您已登录',
					icon:"none"
				})
				uni.switchTab({
					url: '/pages/user/user'
				})
				
			}, 2000);
		}
		// #endif
		},
	methods: {
		wxlogin(){
			// #ifdef H5
			let h5token = new H5Token();
				h5token.verify('userinfo'); //静默获取openid		
			// #endif
			// #ifdef APP-PLUS || MP-WEIXIN
			this.auth.is_name = !this.auth.is_name
			// #endif
		},
		smslogin(){
			
		},
		check_login(e) {
			let token = uni.getStorageSync('token');
			let my = uni.getStorageSync('my');
			console.log('token:'+token)
			console.log('my:'+JSON.stringify(my))
			uni.switchTab({
				url: '/pages/user/user'
			})
			},
		back() {
			uni.navigateBack();
		},
		inputCode(e) {
			this.code = e.detail.value;
			console.log(this.code)
		},
		inputMobile: function(e) {
			this.mobile = e.detail.value;
			console.log(this.mobile)
		},
		inputPwd: function(e) {
			this.password = e.detail.value;
		},
		clearInput(type) {
			if (type == 1) {
				this.mobile = '';
			} else {
				this.password = '';
			}
		},
		login:function(){
			let that = this
			let mobile = that.mobile
			let password = that.password
			let come_from = this.$come_from
			let data={from:come_from,mobile:mobile,password:password,loginType:2}
			console.log('come_from:'+come_from)
			this.$H.post('sapi/user/login',data).then(res => { 
				if(res.status == 200){
					uni.setStorageSync('token', res.data.token);
					let arr={}
					arr['data'] = res.data.my
					arr['save_time']=time
					uni.setStorageSync('my', arr);
					uni.switchTab({
						url: '/pages/user/user'
					})
				}else if(res.status == 400){
					setTimeout(() => {	
					}, 2000);
					uni.showToast({
						title:res.msg,
						icon:"none"
					})
				}
					
					
						
				})
		},
		
		href(page) {
			
			let url = "";
			switch (page) {
				case 1:
					url = "./verify_mobile"
					break;
				case 2:
					url = "./register"
					break;
					
				case 3:
					url = "./login_sms"
					break;
				default:
					break;
			}
			uni.navigateTo({
				url: url
			})
		},
		showOtherLogin() {
			//打开后 不再关闭
			this.popupShow = true;
		}
	}
};
</script>

<style lang="scss">
.container {
	.tui-status-bar {
		width: 100%;
		height: var(--status-bar-height);
	}

	.tui-header {
		width: 100%;
		padding: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.tui-page-title {
		width: 100%;
		font-size: 48rpx;
		font-weight: bold;
		color: $uni-text-color;
		line-height: 42rpx;
		padding: 40rpx;
		box-sizing: border-box;
		text-align: center;
	}

	.tui-form {
		padding-top: 50rpx;

		.tui-view-input {
			width: 100%;
			box-sizing: border-box;
			padding: 0 40rpx;

			.tui-cell-input {
				width: 100%;
				display: flex;
				align-items: center;
				padding-top: 48rpx;
				padding-bottom: $uni-spacing-col-base;

				input {
					flex: 1;
					padding-left: $uni-spacing-row-base;
				}

				.tui-icon-close {
					margin-left: auto;
				}
			}
		}

		.tui-cell-text {
			width: 100%;
			padding: $uni-spacing-col-lg $uni-spacing-row-lg;
			box-sizing: border-box;
			font-size: $uni-font-size-sm;
			color: $uni-text-color-grey;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tui-color-primary {
				color: $uni-text-color;
			}
		}

		.tui-btn-box {
			width: 100%;
			padding: 0 $uni-spacing-row-lg;
			box-sizing: border-box;
			margin-top: 80rpx;
		}
	}

	.tui-login-way {
		width: 100%;
		font-size: 26rpx;
		color: $uni-text-color;
		display: flex;
		justify-content: center;
		position: fixed;
		left: 0;
		bottom: 220rpx;

		view {
			padding: 12rpx 0;
		}
	}

	.tui-auth-login {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 80rpx;
		padding-top: 20rpx;

		.tui-icon-platform {
			width: 90rpx;
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			margin-left: 40rpx;

			&::after {
				content: '';
				position: absolute;
				width: 200%;
				height: 200%;
				transform-origin: 0 0;
				transform: scale(0.5, 0.5) translateZ(0);
				box-sizing: border-box;
				left: 0;
				top: 0;
				border-radius: 180rpx;
				border: 1rpx solid $uni-text-color-placeholder;
			}
		}

		.tui-login-logo {
			width: 60rpx;
			height: 60rpx;
		}
	}
}
</style>
